<?php


namespace App\controller\choice;


use App\Conf;
use App\controller\JobRegister;
use App\controller\PrinterController;
use App\controller\RegisterController;
use App\controller\ReturnMsg;
use App\server\MsgContent;

class Manage
{

    private $content;

    private $fd;

    private $server;


    const TYPE_JOB = 1;

    const TYPE_PRINTER = 2;

    const TYPE_REGISTER = 3;

    const TYPE_MSG = 4;

    const PING = 5;

    /**
     * @var string[]
     */
    private static $conf = [
        self::TYPE_JOB => JobRegister::class,
        self::TYPE_PRINTER => PrinterController::class,
        self::TYPE_REGISTER => RegisterController::class,
    ];


    /**
     * Manage constructor.
     */
    public function __construct(?MsgContent $data, $fd, \Swoole\Server $server)
    {
        $this->content = $data;
        $this->fd = $fd;
        $this->server = $server;
    }


    /**
     * swithPack
     * @return MsgContent
     * date:2021/12/22
     * time:16:06
     * auth：xyc
     */
    public function swithPack()
    {
        try {
            switch ($this->content->getType()) {
                case self::PING:
                    return new MsgContent(intval($this->content->getType()), "pang");
            }
            $class = self::$conf[$this->content->getType()];
            return new MsgContent(intval($this->content->getType()), ReturnMsg::msg(200, "ok", (new $class($this->content->getData(), $this->fd, $this->server))->returnPackData()));
        } catch (\Throwable $e) {
            return new MsgContent(self::TYPE_MSG, ReturnMsg::msg(500, $e->getMessage()));
        }
    }
}